/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.yonsei.iwb.sdt.guix;

import com.hp.hpl.jena.rdf.model.Resource;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.List;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

/**
 *
 * @author DK
 */
public class LearningInfoViewPanel extends JPanel implements ActionListener, ItemListener{
    
    private GDLearningPanel mf;
    private Controller ctrl;
    private JButton findTargetClassButton;
    private JButton findTargetPropertyButton;
    private JButton setTargetClassButton;
    private JButton setTargetPropertyButton;
    private JButton setTargetInstanceButton;
    private JRadioButton isTargetProperty;
    private JRadioButton isTargetInstance;
    private JTextField targetClassInput;
    private JTextField targetPropertyInput;
    private List<String> instanceset;
    
    public LearningInfoViewPanel(){
        
    }
    
    public LearningInfoViewPanel(GDLearningPanel mf){

        
//        this.setBorder(new TitledBorder("Learning Information"));
        this.setBorder(BorderFactory.createEtchedBorder());
        
        this.mf = mf;
        this.ctrl = this.mf.getController();
        
        JLabel targetClassLabel = new JLabel();
        targetClassLabel.setFont(new java.awt.Font("맑은 고딕", 1, 12));
        targetClassLabel.setText("Target Class");
        
        JLabel targetPropertyLabel = new JLabel();
        targetPropertyLabel.setFont(new java.awt.Font("맑은 고딕", 1, 12));
        targetPropertyLabel.setText("Target Property");
        
        JLabel targetInstanceLabel = new JLabel();
        targetInstanceLabel.setFont(new java.awt.Font("맑은 고딕", 1, 12));
        targetInstanceLabel.setText("Target Instance");
                
        targetClassInput = new JTextField();        
        targetClassInput.addKeyListener(new KeyAdapter(){
            public void keyTyped(KeyEvent e){
                if(e.getKeyChar() == KeyEvent.VK_ENTER){
                    setTargetClassButton.doClick();
                }
            }
        });
        
        targetPropertyInput = new JTextField();
        targetPropertyInput.addKeyListener(new KeyAdapter(){
            public void keyTyped(KeyEvent e){
                if(e.getKeyChar() == KeyEvent.VK_ENTER){
                    setTargetPropertyButton.doClick();
                }
            }
        });
        
        findTargetClassButton = new JButton();
        findTargetClassButton.setText("  Find  ");
        findTargetClassButton.addActionListener(this);        
        
        setTargetClassButton = new JButton();
        setTargetClassButton.setText("   Set   ");
        setTargetClassButton.addActionListener(this);
        
        findTargetPropertyButton = new JButton();
        findTargetPropertyButton.setText("  Find  ");
        findTargetPropertyButton.addActionListener(this);
        
        setTargetPropertyButton = new JButton();
        setTargetPropertyButton.setText("   Set   ");
        setTargetPropertyButton.addActionListener(this);
        
        setTargetInstanceButton = new JButton();
        setTargetInstanceButton.setText("  Find  ");
        setTargetInstanceButton.addActionListener(this);
        
        ButtonGroup bg =new ButtonGroup();
        isTargetProperty = new JRadioButton("",false);
        isTargetInstance = new JRadioButton("",false);
        bg.add(isTargetProperty);
        bg.add(isTargetInstance);

        isTargetProperty.addItemListener(this);
        isTargetInstance.addItemListener(this);
                
        
        GroupLayout learninfoPanelLayout = new GroupLayout(this);
        this.setLayout(learninfoPanelLayout);
        learninfoPanelLayout.setHorizontalGroup(
            learninfoPanelLayout.createSequentialGroup()
                .addGap(15)
                .addGroup(learninfoPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(targetClassLabel)
                .addComponent(targetClassInput, GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)      
                
                .addGroup(learninfoPanelLayout.createSequentialGroup()
                .addComponent(findTargetClassButton)
                .addComponent(setTargetClassButton)
                
                )
                
                )
                .addGap(10)
                
                .addGroup(learninfoPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(isTargetProperty)
                .addComponent(isTargetInstance)          

                )

                .addGroup(learninfoPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(targetPropertyLabel)
                .addComponent(targetPropertyInput, GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)
                .addGroup(learninfoPanelLayout.createSequentialGroup()
                .addComponent(findTargetPropertyButton)
                .addComponent(setTargetPropertyButton)
                
                )
//                .addGap(6)
                .addComponent(targetInstanceLabel)
                .addComponent(setTargetInstanceButton)
                )
                .addGap(15)
                                
        );
        
        learninfoPanelLayout.setVerticalGroup(
            learninfoPanelLayout.createSequentialGroup()
                .addGap(15)
                
                .addGroup(learninfoPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(targetClassLabel)
                .addComponent(isTargetProperty)
                .addComponent(targetPropertyLabel)
                                    
                )
                
                .addGroup(learninfoPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(targetClassInput, GroupLayout.DEFAULT_SIZE, 10, Short.SIZE)
                .addComponent(targetPropertyInput, GroupLayout.DEFAULT_SIZE, 10, Short.SIZE)
                
                

                )

                .addGroup(learninfoPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(findTargetClassButton)
                .addComponent(setTargetClassButton)
                .addComponent(findTargetPropertyButton)
                .addComponent(setTargetPropertyButton)                                
                )
                .addGap(6)

                .addGroup(learninfoPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)                
                .addComponent(isTargetInstance)
                .addComponent(targetInstanceLabel)                
                )
                .addComponent(setTargetInstanceButton)                    
                
        );
    }
    
    public void setTargetClassURI(String s){
//        String result = new String();
        
        this.targetClassInput.removeAll();
        this.targetClassInput.setText(s);
        
//        result = s;
    }
    
    public void setTargetPropertyURI(String s){
        this.targetPropertyInput.removeAll();
        this.targetPropertyInput.setText(s);
    }
    
    public void setInstanceSet(List<String> insset){
        instanceset = insset;
    }
        
    
    @Override
    public void itemStateChanged(ItemEvent e) {
        
        // Target Property를 사용할 것인지 Target Instance를 지정할 것인지 결정
        
        Object ob1 = e.getSource();
        if(e.getStateChange()==ItemEvent.SELECTED){
            if (ob1 == isTargetProperty) {                
                mf.oivp.setVisible(false);                
                mf.outputTextArea.append("Select Target Property\n");            
            } else if (ob1 == isTargetInstance) {                
                mf.oivp.setVisible(true);
                mf.ontInfobtwloptANDtreeSplitPane.setDividerLocation(200);
                mf.outputTextArea.append("Select Target Instance\n");
            }            
            
        }
    }    

    @Override
    public void actionPerformed(ActionEvent e) {
        Object o = e.getSource();
        if(CurrentMainFrameStatus.ONTOLOGY_LOADED){
            if(o == this.findTargetClassButton){
                if(CurrentMainFrameStatus.ONTOLOGY_LOADED){
//                    Vector<String> rset = this.ctrl.getAllInstances();
                    Vector<String> rset = this.ctrl.getClassTypeResource();
                    
                    OntologyURIFindViewFrame oufvf = new OntologyURIFindViewFrame(rset, this, 0);
                    oufvf.showFrame();
                }else{
                    JOptionPane.showMessageDialog(this, "Load Ontology first", "Error", JOptionPane.WARNING_MESSAGE); 
                }
                
            
            }else if(o == this.setTargetClassButton){                
                String s = targetClassInput.getText();
                if(s.replaceAll(" ", "").isEmpty()){
                    JOptionPane.showMessageDialog(this, "Input Target Class URI", "Error", JOptionPane.WARNING_MESSAGE); 
                    targetClassInput.setText("");
                }else{
                    this.ctrl.setTargetClass(s);
                    CurrentMainFrameStatus.TARGETCLASS_SELECTED = true;
                    CurrentMainFrameStatus.TARGETCLASS_JUST_SELECTED = true;
                    this.mf.outputTextArea.append(s+"\n");
                }
            }else if(o == this.findTargetPropertyButton){
                if(CurrentMainFrameStatus.ONTOLOGY_LOADED){
                    if(CurrentMainFrameStatus.TARGETCLASS_SELECTED){
                        Vector<String> rset = this.ctrl.getPropertyResource(targetClassInput.getText());                        
                        OntologyURIFindViewFrame oufvf = new OntologyURIFindViewFrame(rset, this, 1);
                        oufvf.showFrame();     
                    }else{
                        JOptionPane.showMessageDialog(this, "Target Class Loaded first", "Error", JOptionPane.WARNING_MESSAGE); 
                    }                                   
                }else{
                    JOptionPane.showMessageDialog(this, "Load Ontology first", "Error", JOptionPane.WARNING_MESSAGE); 
                }
                    
            }else if(o == this.setTargetPropertyButton){
                String s = targetPropertyInput.getText();
                if(s.replaceAll(" ", "").isEmpty()){
                    JOptionPane.showMessageDialog(this, "Input Target Property URI", "Error", JOptionPane.WARNING_MESSAGE); 
                    targetPropertyInput.setText("");
                }else{
                    this.ctrl.setTargetProperty(s);
                    CurrentMainFrameStatus.TARGETPROPERTY_SELECTED = true;
                    CurrentMainFrameStatus.TARGETINSTANCE_SELECTED = false;
                    this.mf.outputTextArea.append(s+"\n");
                }                
            }else if(o == this.setTargetInstanceButton){                
                if(CurrentMainFrameStatus.TARGETCLASS_SELECTED){
                    if(this.mf.oivp.listFilled()){
                        if(CurrentMainFrameStatus.TARGETCLASS_JUST_SELECTED){
                            this.mf.oivp.clearTargetInsList();
//                            Vector<Resource> rset = this.ctrl.getTargetInstances();
//                            this.mf.oivp.addTargetInstanceValue(rset);
                            this.mf.oivp.addTargetInstanceValue(instanceset);
                        }else{
                            this.mf.oivp.showFrame();
                        }
                        
                    }else{
                        if(CurrentMainFrameStatus.TARGETCLASS_JUST_SELECTED){
//                            Vector<Resource> rset = this.ctrl.getTargetInstances();
//                            this.mf.oivp.addTargetInstanceValue(rset);
                            this.mf.oivp.addTargetInstanceValue(instanceset);
                            CurrentMainFrameStatus.TARGETPROPERTY_SELECTED = false;
                            CurrentMainFrameStatus.TARGETINSTANCE_SELECTED = true;
                            CurrentMainFrameStatus.TARGETCLASS_JUST_SELECTED = false;                            
                        }
                    }
                }else{
                    JOptionPane.showMessageDialog(this, "Target Class Loaded first", "Error", JOptionPane.WARNING_MESSAGE); 
                }
                
            }
        }else{
            JOptionPane.showMessageDialog(this, "Load Ontology First!!", "Error", JOptionPane.WARNING_MESSAGE); 
        }
    }
    
}
